package com.xiran.srpingboottemplateself.controller;

import com.xiran.srpingboottemplateself.common.Response;
import com.xiran.srpingboottemplateself.pojo.dto.UserRegisterDTO;
import com.xiran.srpingboottemplateself.pojo.entity.User;
import com.xiran.srpingboottemplateself.pojo.vo.LoginReqVO;
import com.xiran.srpingboottemplateself.pojo.vo.LoginRspVO;
import com.xiran.srpingboottemplateself.pojo.vo.UserVO;
import com.xiran.srpingboottemplateself.service.AuthService;
import com.xiran.srpingboottemplateself.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "用户认证模块")
@RestController
@RequestMapping("/api/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final AuthService authService;
    private final UserService userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Response<LoginRspVO> login(@Valid @RequestBody LoginReqVO loginReqVO) {
        log.info("用户登录请求参数 {}", loginReqVO);
        LoginRspVO loginRspVO = authService.login(loginReqVO);

        return Response.success(loginRspVO);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Response<?> register(@Validated @RequestBody UserRegisterDTO registerDTO) {
        // 检查用户名是否已存在
        if (userService.findByUsername(registerDTO.getUsername()) != null) {
            return Response.fail("用户名已存在");
        }
        
        // 转换为User实体
        User user = new User();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(registerDTO.getPassword());
        user.setNickname(registerDTO.getUsername()); // 使用用户名作为昵称
        user.setEmail(registerDTO.getEmail());
        
        Boolean success = userService.registerUser(user);
        return success ? Response.success("注册成功") : Response.fail("注册失败");
    }

    @ApiOperation("获取当前用户信息")
    @GetMapping("/profile")
    public Response<UserVO> getProfile(Authentication authentication) {
        String username = authentication.getName();
        User user = userService.findByUsername(username);
        if (user == null) {
            return Response.fail("用户不存在");
        }
        
        UserVO userVO = userService.getUserProfile(user.getId());
        log.info("获取用户信息成功，用户ID：{}", user.getId());
        log.info("用户信息 {}", userVO);
        return Response.success(userVO);
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/profile")
    public Response<?> updateProfile(@RequestBody User user, Authentication authentication) {
        String username = authentication.getName();
        User currentUser = userService.findByUsername(username);
        if (currentUser == null) {
            return Response.fail("用户不存在");
        }
        
        // 只允许更新昵称和邮箱，不允许修改用户名和密码
        currentUser.setNickname(user.getNickname());
        currentUser.setEmail(user.getEmail());
        
        boolean success = userService.updateUser(currentUser);
        return success ? Response.success("更新成功") : Response.fail("更新失败");
    }
}